<?php

namespace App\Http\Resources\user;

use App\Models\posts\UserFollow;
use App\Models\User;
use Illuminate\Http\Resources\Json\Resource;
use Illuminate\Support\Facades\DB;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class UserResource extends Resource
{
    public function __construct(User $resource)
    {
        $this->resource = $resource;
    }

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $uri = url('yaoqing',['id'=>$this->resource->uuid]);
        if (!is_file(public_path("/code/".config('app.version').'-'.$this->resource->id.".png"))){
            QrCode::format('png')->size(600,600)->generate($uri,public_path("/code/".config('app.version').'-'.$this->resource->id.".png"));

        }

        $url = url("/code/".config('app.version').'-'.$this->resource->id.".png");

        $superior_uuid =  $this->resource->superior_uuid;

        if(isInt($this->resource->superior_id) && empty($this->resource->superior_uuid)){
            $superior_uuid = User::whereId($this->resource->superior_id)->value('uuid');
            $this->resource->superior_uuid = $superior_uuid;
            $this->resource->save();
        }

        $is_follow = 0;
        if ($this->resource->id != auth('api')->id()){

            $map['user_id'] = auth('api')->id();
            $map['follow_id'] = $this->resource->id ;
            $map['status'] = 1;
            $is_follow = UserFollow::where($map)->first() ? 1:0 ;
        }

        return [
            'id' => $this->resource->id,
            'name' => $this->resource->name,
            'uuid' => $this->resource->uuid,
            'header_img' => $this->resource->header_img,
            'phone' => $this->resource->phone??'',
            'money' => $this->resource->money,
            'level_text' => $this->resource->level_text,
            'birthday' => $this->resource->birthday??'',
            'level' => $this->resource->level,
            'cate_text' => $this->resource->cate_text,
            'cate' => $this->resource->cate,
            'sex' => $this->resource->sex,
            'QrCode' => $url,
            'uri'   => $uri,
            'superior_id' => $this->resource->superior_id??0,
            'superior_uuid' => $superior_uuid??0,
            'superior_name' => $this->resource->superior->name??'',
            'is_yys' => $this->resource->is_yys??0,
            'is_follow' => $is_follow,
            'brief' => $this->resource->brief??'',
            'school' => $this->resource->school??'',
            'bg' => $this->resource->bg??'',
            'third' => [
                'id' => $this->resource->third->id??0,
                'user_id' => $this->resource->third->user_id??0,
                'open_id' => $this->resource->third->open_id??'',
                'app_type' => $this->resource->third->app_type??0,
                'app_name' => $this->resource->third->app_name??'',
            ]
        ];

    }
}
